<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			/*
			  Promise(承诺):异步通讯
			  语法:
			  new Promise(function(resolve, reject) {
                   异步执行的代码
			  })
			  
			  resolve:成功时回调函数名
			  reject:失败时回调函数名
			  
			  方法:
			  then(参数1,参数2)
			  参数1:成功时执行的回调函数
			  参数2:失败时执行的回调函数
			*/

			/* let p = new Promise(function(resolve, reject) {
				setTimeout(() => {
					if (true) {
						resolve("成功");
					} else {
						reject("失败");
					}
				}, 3000)
			});

			p.then(data => {
				console.log(data);
			}, error => {
				console.log(error)
			}) */

			let p2 = new Promise((resolve, reject) => {
				var xhr = new XMLHttpRequest();
				xhr.open("GET", "http://poetry.apiopen.top/sentences");
				xhr.send();
				xhr.onreadystatechange = function() {
					if (xhr.readyState == 4 && xhr.status == 200) {
						resolve(xhr.responseText);
						console.log("aaa111");
					} else {
						reject(xhr.responseText);
						console.log("bbb111");
					}
				}
			})

			p2.then(val => {
				console.log("aaa"+val);
			}, val => {
				console.log("bbb"+val);
			})
		</script>
	</body>
</html>
